#include <spi_sim.h>

using uint32_t = uint32_t;
#define TM7707_GAIN_1 0x00
#define TM7707_GAIN_2 0x01
#define TM7707_GAIN_4 0x02
#define TM7707_GAIN_8 0x03
#define TM7707_GAIN_16 0x04
#define TM7707_GAIN_32 0x05
#define TM7707_GAIN_64 0x06
#define TM7707_GAIN_128 0x07

class tm7707 {
public:
    tm7707(spi_io_def *spi, int drdy, int resetPin,bool vref_int);

    void begin();

    void set_max(float *val);

    void get_vout(float *val);

    void set_gain(uint8_t val);

    //! \brief 设定工程值范围最小
    void set_min(float *val);

    //! \brief 读取原始
    int get_raw();

    int loop();

    uint32_t last_tick;
    int _raw, _gain, _status, _freq, _times, _ok, _com_val, _speed;
    int8_t _drdy = 0, _reset = 0, _single, _step, _ch;
    float _max, _min;
    spi_io_def *_spi;

    void powerDown();

    void set_channel(int ch);

private:
    int busy();

    void TM7707_SelfCalibration(uint8_t uiChannelIndex);

    int32_t read(uint8_t uiChannelIndex);

    void init(uint8_t ch);

    void reset();

    bool _avilable;

    bool use_vint;
};
